﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Enter.Entity;
using Enter.Common;
using Enter.BizLogic;
using Enter.Web.SystemAdmin;
using Enter.Config;
namespace WebSystemAdmin.GoodClass
{
    public partial class GoodClassEdit :Page
    {
        public int CID = RequestStrHelper.GetIntFromRequest("CID",0);
        public string ParentID = RequestStrHelper.GetStringFromRequest("ParentID", "");
        T105_GoodClassBLLSub bll = new T105_GoodClassBLLSub();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                ListCtrlHelper.BindEnumOnListControl(ddl_Status, typeof(EnumStatus));
                if (CID!=0)
                {
                    GetInfo();
                }
                else
                {
                    txt_NO.Text = string.IsNullOrEmpty(ParentID) ? "" : ParentID;
                    this.ddl_Status.Items.FindByValue(((byte)EnumStatus.Valid).ToString()).Selected = true;
                }
                lbl_ParentID.Text = string.IsNullOrEmpty(ParentID) ? "无" : bll.GetObjectByNo(ParentID).CName;

            }
        }

        private void GetInfo()
        {
           
            T105_GoodClassEntity model = bll.GetObjectByCID(int.Parse(Request.QueryString["CID"]));
            if (model == null) return;
            txt_NO.Text = model.CNO;
            txt_CName.Text = model.CName.ToString();
            txt_Weight.Text = model.Weight.ToString();

           this.ddl_Status.Items.FindByValue(((byte)model.Status).ToString()).Selected = true;
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            try
            {


                T105_GoodClassEntity model = new T105_GoodClassEntity();

                if (CID != 0)
                {
                    model.CID = CID;
                }
                model.CNO = txt_NO.Text;
                model.CName = txt_CName.Text.Trim();
                if (FUpload.HasFile)
                {

                    UploadUnit Upload = new UploadUnit(ImgPathConfig.GoodClassUploadPath);
                    string fileName = Upload.UpLoadImg(FUpload);
                    model.icon = fileName;
                }
                //else
                //{
                //    if (ParentID != 0)
                //    {
                //        Utils.ShowMessage("请选择图片");
                //        return;
                //    }
                //}

                model.Weight = int.Parse(txt_Weight.Text);
                model.ParentID = ParentID;

                model.Status = (EnumStatus)byte.Parse(ddl_Status.SelectedValue);
                if (bll.Save(model))
                {
                    Utils.throwErrorGotoURL("添加成功", "GoodClassList.aspx");
                }
                else
                {
                    Utils.ShowMessage("添加失败");
                }
            }
            catch (Exception ex)
            {

                Utils.ShowMessage("添加失败:"+ex.Message);
            }
        }

    
    }
}
